package com.example.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.dao.IPythonDao;
import com.example.entity.Python;
import com.example.entity.python.*;
import com.example.service.IPythonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author ChenXiaoBin
 * on 2019-10-15
 */
@Service
public class PythonServiceImpl extends ServiceImpl<IPythonDao, Python> implements IPythonService {

    @Autowired
    private IPythonDao pythonDao;

    @Override
    public void getResource(GetResourceDto getResourceDto) {
        Boolean bool = true;
        String key = null;
        String url = getResourceDto.getUrl();
        //存放请求头，可以存放多个请求头
        HashMap<String, String> headers = new HashMap<>();
        String head = getResourceDto.getHead();
        headers.put("cookie", head);
        String result = HttpUtil.createRequest(Method.GET, url).addHeaders(headers).execute().body();
        PythonData bo = JSON.parseObject(result, PythonData.class);
        if (bo.getErrmsg().equals("ok")) {
            PythonBo pythonBo = JSON.parseObject(bo.getData(), PythonBo.class);
            Map<String, Resource> resourceList = pythonBo.getResourceList();
            key = pythonBo.getFirstResourceId();
            do{
               Resource resource = resourceList.get(key);
               Python python = new Python();
               python.setPythonId(resource.getId());
               python.setNext(resource.getNext());
               python.setType(resource.getType());
               python.setText(resource.getText());
               if (resource.getType() == 6) {
                   String cutUrl = Arrays.asList(resource.getUrl().split("\\?")).get(0);
                   python.setUrl(cutUrl);
               }
               if (resource.getType() == 24 || resource.getType() == 22) {
                   python.setCode(resource.getCode());
               }
               if (resource.getType() == 25) {
                   python.setTitle(resource.getTitle());
               }
               if (!resource.getNext().equals("")) {
                   key = resource.getNext();
               }
               if (resource.getType() == 7) {
                   bool = false;
               }
               python.setCreateTime(new Date());
               python.setNounNum(getResourceDto.getNounNum());
               python.setCourseName("Python爬虫精进");
               pythonDao.insert(python);
                //选择题
                if (resource.getType() == 5) {
                    key = resource.getOptions().get(0).getNext();
                    for (Options options : resource.getOptions()) {
                        Python pythonItem = new Python();
                        pythonItem.setItem(options.getItem());
                        pythonItem.setItemText(options.getText());
                        pythonItem.setPoint(options.getPoint());
                        pythonItem.setItemNext(options.getNext());
                        pythonItem.setNounNum(getResourceDto.getNounNum());
                        pythonItem.setCourseName("Python爬虫精进");
                        pythonDao.insert(pythonItem);
                    }
                }
            } while (bool);
        }

    }

    @Override
    public List<PythonExcel> selectAll(Integer id, String courseName) {
        return pythonDao.setlectAll(id, courseName);
    }


    /**
     * 接送解析
     * @param jsonMessage
     */
    @Override
    public void jsonAnalysis(String jsonMessage) {

//        String messSpil = "3:第三天:www.baiud 和网易www.wangye";
//        String last = messSpil.substring(6, 9);


        JsonText jsonText = JSON.parseObject(jsonMessage, JsonText.class);
        Map map = JSON.parseObject(jsonText.getEntityMap());
        List<Blocks> blocks = JSON.parseArray(jsonText.getBlocks(), Blocks.class);

        List<Content> contentList = new ArrayList<>();
        for (Blocks blo : blocks) {
            Content content = new Content();
            List<SectionMessage> section = new ArrayList<>();
            List<EntityRanges> entityRanges = JSON.parseArray(blo.getEntityRanges(), EntityRanges.class);



            if (entityRanges.size() > 0) {
                Integer beforeLend = 0;
                List<SpileMessage> spileMessages = new ArrayList<>();
                for (int i = 0; i<entityRanges.size(); i++) {
                    Object mapValue= map.get(entityRanges.get(i).getKey());
                    MapValue mapValue1 = JSON.toJavaObject((JSON) mapValue, MapValue.class);
                    entityRanges.get(i).setType(mapValue1.getType());
                    if (i==0) {
                        SectionMessage firstSection = new SectionMessage();
                        String firstContent = blo.getText().substring(beforeLend, Integer.valueOf(entityRanges.get(i).getOffset()));
                        firstSection.setContent(firstContent);
                        firstSection.setType("Text");
                        if (firstContent != null) {
                            section.add(firstSection);
                        }
                        beforeLend = Integer.valueOf(entityRanges.get(i).getOffset());
                    }

                    if (beforeLend < Integer.valueOf(entityRanges.get(i).getOffset())) {
                        SectionMessage beforeSection = new SectionMessage();
                        String beforeContent = blo.getText().substring(beforeLend, Integer.valueOf(entityRanges.get(i).getOffset()));
                        beforeSection.setContent(beforeContent);
                        beforeSection.setType("Text");
                        section.add(beforeSection);
                    }
                    SectionMessage middleSection = new SectionMessage();
                    String middleContent = blo.getText().substring(Integer.valueOf(entityRanges.get(i).getOffset()), Integer.valueOf(entityRanges.get(i).getOffset())
                            + Integer.valueOf(entityRanges.get(i).getLength()));
                    middleSection.setContent(middleContent);
                    middleSection.setType(mapValue1.getType());
                    section.add(middleSection);

                    beforeLend = Integer.valueOf(entityRanges.get(i).getOffset())
                            + Integer.valueOf(entityRanges.get(i).getLength());

                    if (i==entityRanges.size()-1) {
                        SectionMessage lastSection = new SectionMessage();
                        String lastContent = blo.getText().substring(Integer.valueOf(entityRanges.get(i).getOffset())
                                + Integer.valueOf(entityRanges.get(i).getLength()));
                        if (!lastContent.equals("")) {
                            lastSection.setContent(lastContent);
                            lastSection.setType("Text");
                            section.add(lastSection);
                        }
                    }

                }
            } else {
//                List<SectionMessage> section = new ArrayList<>();
                SectionMessage sectionMessage = new SectionMessage();
                sectionMessage.setType("text");
                sectionMessage.setContent(blo.getText());
                section.add(sectionMessage);
            }
            blo.setEnRanges(entityRanges);
            content.setSection(section);
            contentList.add(content);
          }
    }
}
